//
// Created by 麻再挺 on 2021/12/22.
//

#include "sort_menu.h"
#include "straight_sort.h"
#include "shell_sort.h"
#include "bubble_sort.h"
#include "quick_sort.h"
#include "select_sort.h"
#include "merge_sort.h"
#include "heap_sort.h"

/**
 * 直接插入排序
 */
void straight_run();

/**
 * 折半插入排序
 */
void b_straight_run();

/**
 * 二路排序
 */
void t_straight_run();

/**
 * 希尔排序
 */
void shell_run();

/**
 * 冒泡排序
 */
void bubble_run();

/**
 * 快速排序
 */
void quick_run();

/**
 * 选择排序
 */
void select_run();

/**
 * 归并排序
 */
void merge_run();

/**
 * 堆排序
 */
void heap_run();

void sort_run() {
    // 菜单
    int menu = -1;
    // 是否运行
    Status is_run = TRUE;
    while (is_run) {
        printf("╔═══════════════════════════════════════════════╗\n");
        printf("║                     内部排序                  ║\n");
        printf("║ 1: 直接插入排序                               ║\n");
        printf("║ 2: 折半插入排序                               ║\n");
        printf("║ 3: 二路排序                                   ║\n");
        printf("║ 4: 希尔排序                                   ║\n");
        printf("║ 5: 冒泡排序                                   ║\n");
        printf("║ 6: 快速排序                                   ║\n");
        printf("║ 7: 选择排序                                   ║\n");
        printf("║ 8: 归并排序                                   ║\n");
        printf("║ 9: 堆排序                                     ║\n");
        printf("║ 0: 退出                                       ║\n");
        printf("╚═══════════════════════════════════════════════╝\n");
        printf("请选择功能菜单: ");
        fflush(stdout);
        scanf("%d", &menu);

        switch (menu) {
            case 0:
                // 退出
                is_run = FALSE;
                break;
            case 1:
                straight_run();
                break;
            case 2:
                b_straight_run();
                break;
            case 3:
                t_straight_run();
                break;
            case 4:
                shell_run();
                break;
            case 5:
                bubble_run();
                break;
            case 6:
                quick_run();
                break;
            case 7:
                select_run();
                break;
            case 8:
                merge_run();
                break;
            case 9:
                heap_run();
                break;
            default:
                printf("输入异常\n");
                break;
        }
    }
}

void straight_run() {
    int a[8] = {3, 1, 7, 5, 2, 4, 9, 6};
    Straight_Insert_Sort(a, 8);
}

void b_straight_run() {
    int a[8] = {3, 1, 7, 5, 2, 4, 9, 6};
    B_Straight_Insert_Sort(a, 8);
}

void t_straight_run() {
    int a[8] = {3, 1, 7, 5, 2, 4, 9, 6};
    int temp[8];
    Two_Straight_Insert_Sort(a, temp, 8);
    for (int i = 0; i < 8; i++) {
        printf("%d ", a[i]);
    }
    printf("\n");
}

void shell_run() {
    int arr[] = {1, 4, 2, 7, 9, 8, 3, 6};
    Shell_Sort(arr, 8);
    for (int i = 0; i < 8; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

void bubble_run() {
    int arr[8] = {49, 38, 65, 97, 76, 13, 27, 49};
    bubble_sort(arr, 8);
    for (int i = 0; i < 8; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

void quick_run() {
    int arr[8] = {49, 38, 65, 97, 76, 13, 27, 50};
    quick_sort(arr, 0, 7);
    for (int i = 0; i < 8; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

void select_run() {
    int arr[8] = {49, 38, 65, 97, 76, 13, 27, 50};
    select_sort(arr, 8);
    for (int i = 0; i < 8; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

void merge_run() {
    int a[10] = {5, 2, 6, 0, 3, 9, 1, 7, 4, 8};
    merge_sort(a, 10);
    printf("排序后的结果是：");
    for (int i = 0; i < 10; i++) {
        printf("%d ", a[i]);
    }
    printf("\n");
}

void heap_run() {
    int arr[8] = {49, 38, 65, 97, 76, 13, 27, 50};
    heap_sort(arr, 8);
    printf("排序后的结果是：");
    for (int i = 0; i < 8; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}
